//
//  ANShoppingCarCell.m
//  Ants
//
//  Created by ZHM on 16/8/30.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANShoppingCarCell.h"
#import "ANShoppingCarCellFrame.h"
#import "ANChangeCountView.h"

@interface ANShoppingCarCell ()<UITextFieldDelegate>

@property (nonatomic,strong) UIButton *selectBt;
@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic,strong) UILabel *nameLabel;
@property (nonatomic,strong) UILabel *priceLabel;
@property (nonatomic, strong) ANChangeCountView *changeView;
@property (nonatomic,strong) UILabel *soldOutLab;

//这里商品剩余数量 10个
#define ANtotalCount 10

@end


@implementation ANShoppingCarCell

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *cellIdentifier = @"ShoppingCarCell";
    ANShoppingCarCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[ANShoppingCarCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)prepareForReuse
{
    [super prepareForReuse];
//   这里清理重用数据

}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        //添加所有子控件
        [self addUpAllChildView];
        //cell背景颜色
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setModel:(ANShoppingCarCellFrame *)model {
    _model = model;
    _nameLabel.text = model.shoppingItem.product.title;
    _priceLabel.text = model.priceLabelText;
    
    _nameLabel.frame = model.nameLabelFrame;
    _priceLabel.frame = model.priceLabel;
    
    NSDictionary *picDic = [model.shoppingItem.product.icons firstObject];
    NSString *url = picDic[@"minPic"];
    UIImage *placeImage = [UIImage imageWithColor:UIColorFromRGB(0xE7EBEE)];
    [self.icon sd_setImageWithURL: url.toURL placeholderImage:placeImage options:SDWebImageRetryFailed];
    _icon.frame = model.icon;
    
    _selectBt.frame = model.selectBt;
    _selectBt.selected = model.isSelect;
    
    _changeView.frame = model.changeCountView;
    [_changeView setChooseCount:model.count totalCount:[model.shoppingItem.product.quantity integerValue]];
    
    [_changeView.subButton addTarget:self action:@selector(subButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_changeView.addButton addTarget:self action:@selector(addButtonClick) forControlEvents:UIControlEventTouchUpInside];
    _changeView.numberTF.delegate = self;
    
    //这里检测商品数量 为0 显示无货
    if ([model.shoppingItem.product.quantity integerValue] == 0) {
        _soldOutLab.hidden = NO;
    } else {
        _soldOutLab.hidden = YES;
    }
    _soldOutLab.frame = model.soldOutLab;
    
}

- (void)subButtonClick {
    ANLog(@"------");
    _model.count--;
    if (_model.count == 1) {
        _changeView.subButton.enabled = NO;
    } else {
        _changeView.subButton.enabled = YES;
    }
    _changeView.addButton.enabled = YES;
    _changeView.numberTF.text = [NSString stringWithFormat:@"%ld",_model.count];
    if ([self.delegate respondsToSelector:@selector(statTotalPrice:)] && _model.isSelect) {
        [self.delegate statTotalPrice:self];
    }
    [sharedWebService updateShoppingItem:_model.shoppingItem Quantity:_model.count completion:nil];
}
- (void)addButtonClick {
    ANLog(@"++++++");
    _model.count++;
    if (_model.count == [_model.shoppingItem.product.quantity integerValue]) {
        _changeView.addButton.enabled = NO;
    } else {
        _changeView.addButton.enabled = YES;
    }
    _changeView.subButton.enabled = YES;
    _changeView.numberTF.text = [NSString stringWithFormat:@"%ld",_model.count];
    if ([self.delegate respondsToSelector:@selector(statTotalPrice:)] && _model.isSelect) {
        [self.delegate statTotalPrice:self];
    }
    [sharedWebService updateShoppingItem:_model.shoppingItem Quantity:_model.count completion:nil];
}

- (void)addUpAllChildView {
    
    UIImage *btimg = [UIImage imageNamed:@"ic_cb_normal.png"];
    UIImage *selectImg = [UIImage imageNamed:@"ic_cb_checked"];
    _selectBt = [[UIButton alloc]init];
    [_selectBt addTarget:self action:@selector(clickSelect:) forControlEvents:UIControlEventTouchUpInside];
    [_selectBt setImage:btimg forState:UIControlStateNormal];
    [_selectBt setImage:selectImg forState:UIControlStateSelected];
    [self addSubview:_selectBt];
    
    UILabel *nameLable = [[UILabel alloc] init];
    [self addSubview:nameLable];
    nameLable.font = ANDetailLabelSize;
    nameLable.numberOfLines = 0;
    _nameLabel = nameLable;
    
    UILabel *priceLabel = [[UILabel alloc] init];
    priceLabel.font = ANDetailLabelSize;
    priceLabel.textColor = [UIColor orangeColor];
    [self addSubview:priceLabel];
    _priceLabel = priceLabel;

    UIImageView *icon = [[UIImageView alloc] init];
    icon.contentMode = UIViewContentModeScaleAspectFill;
    icon.clipsToBounds = YES;
    icon.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconClick)];
    [icon addGestureRecognizer:tap];
    [self addSubview:icon];
    _icon = icon;
    
    _soldOutLab = [[UILabel alloc]init];
    
    _soldOutLab.text = @"无货";
    _soldOutLab.hidden = YES;
    _soldOutLab.font =  ANDetailLabelSize;
    _soldOutLab.textColor = [UIColor redColor];
    [self addSubview:_soldOutLab];
    
    _changeView = [[ANChangeCountView alloc] init];
    [self addSubview:_changeView];
}
- (BOOL)isPureInt:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return[scan scanInt:&val] && [scan isAtEnd];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([_model.shoppingItem.product.quantity integerValue] == 0) {
        _changeView.numberTF.text = @"0";
        [sharedWebService updateShoppingItem:_model.shoppingItem Quantity:0 completion:nil];
        if ([self.delegate respondsToSelector:@selector(statTotalPrice:)] && _model.isSelect) {
            [self.delegate statTotalPrice:self];
        }
        return;
    }
    if ([self isPureInt:_changeView.numberTF.text]) {
        if ([_changeView.numberTF.text isEqualToString:@""] || [_changeView.numberTF.text integerValue] <= 0) {
            //负数
            _changeView.numberTF.text = @"1";
            _changeView.subButton.enabled = NO;
            _changeView.addButton.enabled = YES;
        } else {
            _changeView.subButton.enabled = YES;
            NSString *numText = _changeView.numberTF.text;
            //数量上限=商品数量
            if ([numText intValue] > [_model.shoppingItem.product.quantity intValue]) {
                _changeView.numberTF.text = [NSString stringWithFormat:@"%ld",[_model.shoppingItem.product.quantity integerValue]];
                _changeView.addButton.enabled = NO;
            }else {
                _changeView.addButton.enabled = YES;
            }
            //限制数量
            //    if ([_changeView.numberTF.text intValue] > 99) {
            //        _changeView.numberTF.text = @"99";
            //    }
//            if ([_changeView.numberTF.text integerValue] == 0) {
//                _soldOutLab.hidden = NO;
//                _changeView.subButton.enabled = NO;
//                _changeView.addButton.enabled = NO;
//            }
        }
    }else {
        //非法数字
        _changeView.numberTF.text = @"1";
        _changeView.subButton.enabled = NO;
    }
    _model.count = [_changeView.numberTF.text integerValue];
    [sharedWebService updateShoppingItem:_model.shoppingItem Quantity:_model.count completion:nil];
    if ([self.delegate respondsToSelector:@selector(statTotalPrice:)] && _model.isSelect) {
        [self.delegate statTotalPrice:self];
    }
}
- (void)iconClick {
    ANLog(@"显示商品");
    if ([self.delegate respondsToSelector:@selector(showProduct:)]) {
        [self.delegate showProduct:_model.shoppingItem.product];
    }
}
-(void)clickSelect:(UIButton *)bt
{
    _selectBt.selected = !_selectBt.selected;
    _model.isSelect = _selectBt.selected;
    if (!_soldOutLab.hidden || self.isEdit) {
        return;
    }

    _model.count = [_changeView.numberTF.text integerValue];

    if ([self.delegate respondsToSelector:@selector(statTotalPriceWithSelectBtn:)]) {
        [self.delegate statTotalPriceWithSelectBtn:self];
    }
    
}

@end
